import {defineStore} from 'pinia'
import {ref} from 'vue'
import {refushLoginApi2} from '../utils/requestApi.js'


//管理登录信息
export const usePersonStore = defineStore("personStore", () => {
const personinfo = ref(null)

	//判断是否登录
	const getLoginStatus = async () => {
		var token = localStorage.egtItem("token")
		if (token == null || token == "") {
			return false;
		}
		if (personinfo.value == null) {
			var data = await refushLoginApi2();

			if (data.result) {
				setPersonInfo(data.data, token);
				return true;
			} else {
				localStorage.removeItem("token")
				return false
			}
		} else {
			return ture;
		}
	}


	const setPersonInfo = (person, token) => {
		//获取到的用户信息存储到当前状态管理
		personinfo.value = person;
		personinfo.value.token = token;
		localStorage.setItem("token", token);
	}

	//退出登录
	const logout = () => {
		personinfo.value = null
		loaclStorage.removeItem("token")
	}
	return {
		personinfo,
		setPersonInfo,
		getLoginStatus,
		logout
	}
})